cmd line tutorials - gencmd

cmd line tutorials - gencmd

The alias Command

Unix-Linux Mac

The alias command creates shortcuts for other commands. This can be useful for a number of reasons, such as:

  • You can type lesser and therefore be more efficient.
  • You don’t have to remember the names of commands that you don’t use very often.
  • You can customize lengthy command options into a few letters.

How to use the alias command

To create an alias, use the following syntax:

alias alias_name=command

For example, to create an alias called ll that runs the ls -l command, you would type:

alias ll=ls -l

Once you have created an alias, you can use it like any other command. For example, to list the contents of the current directory in long format, you would type:

ll

Alias examples

Here are some examples of useful aliases:

alias ll=ls -l - List the contents of the current directory in long format.

alias la=ls -a - List all files and directories, including hidden files.

alias grep=grep --color=auto - Colorize the output of the grep command.

alias df=df -h - Show the disk usage of the current directory in human-readable format.

alias c=clear - Clear the terminal screen.

Tips for using aliases

  • When creating an alias, it is important to choose a name that is easy to remember and type.
  • You can use aliases to combine multiple commands into a single command. For example, you could create an alias called update that runs the following commands:

alias update=sudo apt update && sudo apt upgrade

  • You can also use aliases to create custom functions. For example, you could create an alias called backup that creates a backup of your home directory:

alias backup=tar -cvf ~/backup.tar /home/$USER

Managing aliases

To list all of your current aliases, you can use the following command:

alias

To remove an alias, you can use the following command:

unalias alias_name

If you are using some commands regularly, add it to the configuration file specific to your shell. To do this, create a file called .bashrc (or .profile or .zshrc or another one depending on your shell) in your home directory and add your aliases to the file. For example:

alias ll=ls -l
alias la=ls -a

Once you have saved the file, close and reopen your terminal session. Your aliases will now be loaded automatically.

With gencmd

gencmd -c alias ls

  • alias ls=‘ls –color=auto’
  • alias ls=“ls –color=auto –group-directories-first”

gencmd -c alias remove alias

  • unalias [-a] [name …]
  • unalias [name]

Conclusion

The alias command can make you productive on the command line with custom abbreviations. By creating aliases for frequently used commands, you can save time and effort.